package com.ziyucoding.springboot.labs.lab3.rocketmqdemo.consumer;

import com.ziyucoding.springboot.labs.lab3.rocketmqdemo.message.Demo6Message;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 示例6的消费者（用于顺序消息）
 *
 * @author ziyucoding
 * @date 2022-10-24
 **/
@Component
@RocketMQMessageListener(
        topic = Demo6Message.TOPIC,
        consumerGroup = "demo6-consumer-group-" + Demo6Message.TOPIC,
        consumeMode = ConsumeMode.ORDERLY // 设置为顺序消费
)
public class Demo6Consumer implements RocketMQListener<Demo6Message> {

    private Logger logger = LoggerFactory.getLogger(Demo6Consumer.class);

    @Override
    public void onMessage(Demo6Message message) {
        logger.info("[onMessage][线程编号：{} 消息内容：{}]", Thread.currentThread().getId(), message);
        // sleep 1 秒，用于查看顺序消费的效果
        try {
            Thread.sleep(1 * 1000L);
        } catch (InterruptedException ignore) {
        }
    }
}
